package com.yidiandao.business.ui.main.shoppingcart.inventory;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.yidiandao.business.R;
import com.yidiandao.business.base.SuperBaseFragment;
import com.yidiandao.business.ui.adapter.BuyGoodsListAdapter;
import com.yidiandao.business.ui.widget.listview.DisplayListItem;
import com.yidiandao.business.utils.UIUtils;

import java.util.List;

/**
 * 确认订单-商品列表-View
 */
public class InventoryFragment extends SuperBaseFragment
        implements InventoryContract.View {

    private InventoryContract.Presenter mPresenter;

    private Toolbar mToolbar;

    private ListView mListView;

    private TextView mTvTotalNumber;

    private BuyGoodsListAdapter mAdapter;

    public static InventoryFragment getInstance(Bundle extras) {
        InventoryFragment fragment = new InventoryFragment();
        fragment.setArguments(extras);
        return fragment;
    }

    @Override
    public void setPresenter(InventoryContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public String getPagerTag() {
        return InventoryFragment.class.getSimpleName();
    }

    @Override
    public void showToast(String text) {
        UIUtils.showToast(text);
    }

    @Override
    protected void initViews() {
        mToolbar = (Toolbar) mRootView.findViewById(R.id.toolbar);
        mListView = (ListView) mRootView.findViewById(R.id.list_view);
        mTvTotalNumber = (TextView) mRootView.findViewById(R.id.tv_total_number);

        mAdapter = new BuyGoodsListAdapter(mActivity);
        mListView.setAdapter(mAdapter);

        initListeners();
    }

    @Override
    protected void startLoadData() {
        mPresenter.start();
    }

    private void initListeners() {
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mActivity.finish();
            }
        });
    }

    @Override
    public void setDisplayListItems(List<DisplayListItem> displayListItems) {
        mAdapter.setData(displayListItems);
    }

    @Override
    public void setTotalNumber(int totalGoodsNumber) {
        mTvTotalNumber.setText(UIUtils.getString(R.string.buy_goods_count, totalGoodsNumber));
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_inventory;
    }

    @Override
    public void refreshContent(int result) {
        superRefreshLayoutState(result);
    }

    @Override
    public int checkResultObject(Object resultObject) {
        return superCheckListState(resultObject);
    }
}
